<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Cache-Control" content="no-siteapp,no-transform,no-store, no-cache, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="content" id="view"></div>
<script id="templateId" type="text/html">
    <div class="layui-tab" lay-filter="demo">
        <ul class="layui-tab-title">
            {{# layui.each(d,function(index,item){ }}
            <li class="{{index==currIndex ? 'layui-this' : '' }}"
                data-name="{{getNameByUrl(item.url)}}">{{item.title}}
            </li>
            {{# }) }}
        </ul>
        <div class="layui-tab-content">
            {{# layui.each(d,function(index,item){ }}
            <div class="layui-tab-item {{index==currIndex ? 'layui-show' : '' }}">
                <iframe id="{{getNameByUrl(item.url)}}" class="iframe{{index}}"
                        src=""
                        frameborder="0"
                        style="width:100%;height:100%;"></iframe>
            </div>
            {{# }) }}
        </div>
    </div>
</script>

<script src="/static/js/main.js"></script>
<script type="text/javascript">
    window.getNameByUrl = function (url) {
        var res = url.split('.html')[0].split('/')[4];
        return res;
    };
    window.resetTab = function (name, count) {
        if ($('li[data-name=' + name + '] .layui-badge')) {
            $('li[data-name=' + name + '] .layui-badge').remove();
        }
        var thisLi = $('li[data-name=' + name + ']');
        var html = thisLi.html();
        html += ' <span class="layui-badge">' + count + '</span>';
        thisLi.html(html);
    };
    function getData() {
        return new Promise(function(resolve,reject){
            $('.layui-tab-content>.layui-tab-item>iframe').each(function(index,item){
                try{
                    $(item)[0].contentWindow.getData().then(function(res){
                        resolve(res);
                    });
                }catch(err){}
            });
        })
    }
    layui.use(['jquery', 'templateInit', 'tabHeight', 'element', 'configureCom', 'configure', 'bluebird'], function () {
        var element = layui.element;
        var templateInit = layui.templateInit;
        var tabHeight = layui.tabHeight;
        window.configure = layui.configure;
        window.configureCom = layui.configureCom;
        window.layer = layui.layer;
        window.$ = window.jQuery = layui.$;

        window.currIndex = 0;//默认打开的页面
        var tabData = JSON.parse(localStorage.tabData);
        var templateHtml = $('#templateId').html();


        templateInit.init('view', tabData, templateHtml);
        element.render();
        tabHeight.init();

        function refreshTabNum() {
            $.each(tabData, function (index, item) {//获取数量
                if (item.numUrl) {
                    configureCom.getAjax(item.numUrl, {page: 1, size: 1}).then(function (res) {
                        var name = getNameByUrl(item.url);
                        var count = res.result.totalElements;
                        resetTab(name, count);
                    })
                }
            });
        }

        refreshTabNum();

        $('#view iframe').eq(currIndex).attr('src', configureCom.nocache(tabData[currIndex].url));
        var indexObj = {};
        indexObj['li' + currIndex] = true;


        element.on('tab(demo)', function (res) {
            var className = 'li' + res.index;
            indexObj[className] = true;
            $('#view iframe').eq(res.index).attr('src', configureCom.nocache(tabData[res.index].url));
        });
        window.refreshIframe = function (name) {
            $.each(tabData, function (index, item) {
                if (getNameByUrl(item.url) == name) {
                    try {
                        $('#' + getNameByUrl(item.url))[0].contentWindow.tableRender();
                    } catch (e) {

                    }
                } else {
                    $('#' + getNameByUrl(item.url))[0].contentWindow.location.reload();
                    refreshTabNum();
                }
            });
        }
    });

</script>
</body>
</html>